package com.hrserviceserve.service;

import com.hrserviceserve.dao.TrainingRepository;
import com.hrserviceserve.dao.TransferRepository;
import com.hrserviceserve.entity.Result;
import com.hrserviceserve.entity.Training;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Map;

@Service
public class TrainingService {
    @Autowired
    TrainingRepository trainingRepository;

    public Result findByPage(Map<String,Object> map){
        PageRequest pageRequest=PageRequest.of((int)map.get("page")-1,(int)map.get("limit"));
        Page<Training> page=trainingRepository.findAllWhere((Integer) map.get("personnelId"),(String)map.get("trainingCourse"),pageRequest);
        return Result.success(page);
    }

    public Result save(Training training){
        Training save = trainingRepository.save(training);
        if (save!=null){
            return Result.success(save);
        }else {
            return Result.fail();
        }
    }
    @Transactional
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    public int delete(Integer id){
        return trainingRepository.deleteBytrainingID(id);
    }
}
